home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / UIFlow 1.0.1 / UIFlow Source / CFDFront / TDataTransportMechanism.cp < prev    next >
Encoding:
Text File  |  1992-02-21  |  14.7 KB  |  513 lines  |  [TEXT/MPS ]

  1. #include "dtm.h"
  2. #include "sds.h"
  3.  
  4. // **********************************************************************
  5. //    raster image data set. All we will do with it for now is write it.
  6. // **********************************************************************
  7. class TRasterData : public TObject
  8.     {
  9.     private:
  10.         char **        fRstr;
  11.         int                fDims[2];
  12.         char             fTitle [128];
  13.     
  14.     public:
  15.         // This routine will write the scientific data out to an
  16.         // hdf file.
  17.         WriteRIS ();
  18.         
  19.     };
  20.     
  21. // **********************************************************************
  22. //    this is the data transport mechanism. 
  23. // **********************************************************************
  24. class TDataTransport : public TObject
  25.     {
  26.     private:
  27.         TDocument    *    fDocument;
  28.         short        fInport;
  29.         short        fOutport;
  30.         short        fRexecSocket;
  31.         char            fHeader[DTM_MAX_HEADER];
  32.         char            fExpass[80];
  33.         char            fExuser[80];
  34.         char            fExmachine[80];
  35.         
  36.     public:
  37. //     This routine will launch a remote program and establish input and output connections with it
  38.         int LaunchRemote (char * command);
  39.         
  40. //     This routine determines if there is data coming in. If there is it will return it's type. 
  41. //    If there is not data comming in,  it will return 0. If the connection is broken it will return -1.
  42.         int DataReady ();
  43.         
  44. // This routine will read an incoming message and return a pointer to the message.
  45.         int ReceiveMessage (char * message);
  46.         
  47. // This routine will send a command or message to the remote program.
  48.         int SendMessage (char * message);
  49.             
  50.         Boolean GetMachineName(void);
  51.         void IDataTransport(TDocument *);                                                        // initializes data transport
  52.         int ReceiveRasterData (TRasterData *);                                                // reads a raster image group.
  53.         int EndDTM (void);                                                                                // kill the DTM ports. 
  54.     };
  55.     
  56. pascal Boolean pfilt(DialogPtr td,EventRecord * tdevent,short itemhit);
  57. void UItemAssign( DialogPtr dlog, int item, pascal void (*proc)(DialogPtr,short));
  58. Boolean IsCancelEvent();
  59. pascal void OutlineItem(DialogPtr theDialog, short theItem);
  60.  
  61. // --------------------------------------------------------------------------------------------------
  62. //    CancelEvent ()
  63. //                This routine returns true if command-. has been entered.
  64. // --------------------------------------------------------------------------------------------------
  65. Boolean IsCancelEvent()
  66.     {
  67.     EventRecord tmpEvent;
  68.     
  69.     // look for a command-.
  70.     if (EventAvail(keyDownMask, &tmpEvent)) 
  71.         {
  72.         GetNextEvent(keyDownMask, &tmpEvent);
  73.         if (tmpEvent.what == keyDown || tmpEvent.what == autoKey)
  74.             if ((tmpEvent.modifiers & cmdKey) && ((char)(tmpEvent.message & 0xFF) == '.'))
  75.                 return true;
  76.         }
  77.     
  78.     return false;
  79.     }
  80.  
  81. // --------------------------------------------------------------------------------------------------
  82. //    UItemAssign
  83. // --------------------------------------------------------------------------------------------------
  84. void UItemAssign( DialogPtr dlog, int item, pascal void (*proc)())
  85.     {
  86.     Rect        ibox;
  87.     char        **ihndl;
  88.     short        typ;
  89.  
  90.     GetDItem( dlog, item, &typ, &ihndl, &ibox);
  91.     SetDItem( dlog, item,  typ,  (Handle) proc, &ibox);
  92.     }
  93.  
  94. // --------------------------------------------------------------------------------------------------
  95. //    OutlineItem        Outline a dialog item
  96. // --------------------------------------------------------------------------------------------------
  97. pascal void OutlineItem(DialogPtr theDialog, short theItem)
  98.     {
  99.     Rect        ibox;
  100.     char        **ihndl;
  101.     short        typ;
  102.     
  103.     GetDItem( theDialog, theItem, &typ, &ihndl, &ibox);
  104.     PenSize( 3,3);
  105.     InsetRect( &ibox, -4,-4);
  106.     FrameRoundRect( &ibox, 16,16);
  107.     }
  108.  
  109. // **********************************************************************
  110. //    TDataTransport METHODS
  111. // **********************************************************************
  112. // --------------------------------------------------------------------------------------------------
  113. //    GetMachineInfo
  114. //    Displays a dialog box which collects host name, logon name & password from the user
  115. // --------------------------------------------------------------------------------------------------
  116. void TDataTransport::IDataTransport(TDocument * doc)
  117.     {
  118.     fDocument = doc;
  119.     }
  120.     
  121. /*****************************************************************/
  122. /*   Modalproc for prompting which is tied to exremote()
  123. *    This proc makes sure that key echoes are suppressed for item #6
  124. */
  125.  
  126. #define PASSWDITM    (short)6
  127.  
  128. /* special keyboard stuff. */
  129.  
  130. #define KBETX    0x03
  131. #define KBBS    0x08
  132. #define KBHT    0x09
  133. #define KBCR    0x0D
  134. #define KBESC    0x1B
  135.  
  136. //pascal Boolean
  137. //int DTM::pfilt(td,tdevent,itemhit)
  138. pascal Boolean pfilt(DialogPtr td,EventRecord * tdevent,short itemhit)
  139.     {
  140.     short        itemType;
  141.     Handle        itemHdl;
  142.     Rect        itemRect;
  143.     char         s[256];
  144.     int         c,i,len;
  145.     short        insertPt = 32767;
  146.     
  147. /*
  148. *  If it is not the password item, let modaldialog handle it.
  149. */
  150.     
  151.     switch(tdevent->what) {
  152.         case keyDown:
  153.         case autoKey:
  154.             len = strlen(fExpass);
  155. /*
  156. *  If it is CR, take it as OK button, as per Inside Mac
  157. */
  158.             c = (tdevent->message & charCodeMask);
  159.             
  160.             switch (c)
  161.             {
  162.                 /* return or enter. */
  163.                 case KBCR:
  164.                 case KBETX:
  165.                     itemhit = 1;
  166.                     return true;
  167.                     
  168.                 /* cancel (ESC) */
  169.                 case KBESC:
  170.                     itemhit = 2;
  171.                     return true;
  172.                     
  173.                 /* horizontal tab? */
  174.                 case KBHT:
  175.                     return false;
  176.                     
  177.                 /* backspace. */
  178.                 case KBBS:
  179.                     if (((DialogRecord *)td)->editField+1 != PASSWDITM)    
  180.                             return false;    /* only handle passwd field. */
  181.                     
  182.                     if ( (**((DialogRecord *)td)->textH).selStart != (**((DialogRecord *)td)->textH).selEnd)
  183.                         {
  184.                         memmove (&fExpass[(**((DialogRecord *)td)->textH).selStart],
  185.                             &fExpass[(**((DialogRecord *)td)->textH).selEnd], 
  186.                             (**((DialogRecord *)td)->textH).teLength - (**((DialogRecord *)td)->textH).selEnd);
  187.                         len -= (**((DialogRecord *)td)->textH).selEnd - (**((DialogRecord *)td)->textH).selStart;
  188.                         insertPt = (**((DialogRecord *)td)->textH).selStart+1;
  189.                         }
  190.                     else
  191.                         if (len)
  192.                             len--;
  193.                     
  194.                     fExpass[len] = '\0';
  195.                     break;
  196.                 
  197.                 /* a regular character. */
  198.                 default:
  199.                     if (((DialogRecord *)td)->editField+1 != PASSWDITM)    
  200.                             return false;    /* only handle passwd field. */
  201.                     
  202.                     /* move the data to make room for the new entry. */
  203.                     if ( (**((DialogRecord *)td)->textH).selStart != (**((DialogRecord *)td)->textH).selEnd)
  204.                         {
  205.                         memmove (&fExpass[(**((DialogRecord *)td)->textH).selStart+1],
  206.                             &fExpass[(**((DialogRecord *)td)->textH).selEnd], 
  207.                             (**((DialogRecord *)td)->textH).teLength - (**((DialogRecord *)td)->textH).selEnd);
  208.                         len -= (**((DialogRecord *)td)->textH).selEnd - (**((DialogRecord *)td)->textH).selStart;
  209.                         }
  210.                         
  211.                         
  212.                     insertPt = (**((DialogRecord *)td)->textH).selStart+1;
  213.                     if (c > 31)
  214.                     /* legal entry. */
  215.                         if (len < 16) 
  216.                         {
  217.                             fExpass[insertPt-1] = c;
  218.                             fExpass[++len] = '\0';
  219.                         }
  220.                     else 
  221.                     {    /* illegal entry. */
  222.                         SysBeep (3);
  223.                         return true;
  224.                     }
  225.                     break;
  226.  
  227.             }
  228.                                         
  229.                 
  230. /*
  231. *  Clean up after myself, set the Δ echo to the right length.
  232. */
  233.             for (i=0; i< len; i++)
  234.                 s[i] = '•';
  235.             s[len] = 0;
  236.  
  237.             GetDItem(td, PASSWDITM, &itemType, &itemHdl, &itemRect);
  238.             setitext(itemHdl, s);
  239.             SelIText(td, PASSWDITM, insertPt, insertPt);
  240.             return true;
  241.  
  242.         default:
  243.             break;
  244.     }
  245.  
  246.     return(false);                /* default return, unknown key or mouse pressed */        
  247.  
  248. }
  249.  
  250. Boolean TDataTransport::GetMachineName()
  251.     {
  252.     char            s[256];
  253.     DialogPtr    theDialog;
  254.     short        itemType;
  255.     Handle        itemHdl;
  256.     Rect            itemRect;
  257.     short         itemHit,i;
  258.     
  259.     SetCursor(&qd.arrow);
  260.  
  261. // get the dialog box from which the machine name, user name and password will be gotten.
  262.     theDialog = GetNewDialog(138, nil, (WindowPtr) -1);
  263. //    CenterWindow(theDialog);                                                                            // center the dialog.
  264.     
  265.     // this item will be the password item. Handled differently, the characters
  266.     // are hidden.
  267.     GetDItem(theDialog, 6, &itemType, &itemHdl, &itemRect);
  268.     for (i=0; i< strlen(fExpass); i++)                                                                    // echo Δ instead of chars
  269.         s[i] = '•';
  270.     s[i] = 0;
  271.     setitext(itemHdl, s);
  272.     
  273.     // init the other fields, possibly with previously entered name machine
  274.     // and password, if any were entered previously
  275.     GetDItem(theDialog, 5, &itemType, &itemHdl, &itemRect);
  276.     setitext(itemHdl, fExuser);
  277.     GetDItem(theDialog, 4, &itemType, &itemHdl, &itemRect);
  278.     setitext(itemHdl, fExmachine);
  279.     
  280.     SelIText(theDialog, 4, 0,32767);                                                                    // make the selection
  281.     UItemAssign( theDialog, 3, OutlineItem);
  282.         
  283.     do {
  284.         ModalDialog(pfilt, &itemHit);
  285.         } while (itemHit < 1 || itemHit > 2);
  286.  
  287.     if (itemHit == 1)                                                                                             // OK hit, get the info.
  288.         {
  289.         GetDItem(theDialog, 5, &itemType, &itemHdl, &itemRect);
  290.         getitext(itemHdl, fExuser);
  291.         GetDItem(theDialog, 4, &itemType, &itemHdl, &itemRect);
  292.         getitext(itemHdl, fExmachine);
  293.         }
  294.     
  295.     DisposDialog(theDialog);                                                                                // trash the dialog.
  296.     if (itemHit != 1)
  297.         return false;
  298.     else
  299.         return true;
  300.     }
  301.  
  302. // --------------------------------------------------------------------------------------------------
  303. //    LaunchRemote
  304. //    This routine will launch a remote program and establish a connection with it.
  305. // --------------------------------------------------------------------------------------------------
  306.  
  307. // These are made global so that they can be saved.
  308. char hostMachine[256], hostAccount[80],hostPasswd[80];
  309.  
  310. int TDataTransport::LaunchRemote (char * command)
  311. {
  312.     char remoteAddress [512], exec [512];
  313.     int count;
  314.     int currentTick = 0;
  315.     char * pHost;
  316.     
  317.     // get the machine name, login name and passwd.
  318. //    if (!this->GetMachineName ())
  319.         return false;    // user canceled
  320.     
  321.     // get the fInport. This will allow us to dynamically determine what the port
  322.     //    number should be.
  323.     fInport = DTMmakeInPort (":0");
  324.  
  325.     // did we make the inport properly?
  326.     if (fInport == -1)
  327.         return -1;
  328.     
  329.     // get the formal internet address and port number of our inport.
  330.     // the remote will connect to this address for writing.
  331.     DTMgetPortAddr (fInport, remoteAddress, sizeof (remoteAddress));
  332.     sprintf (exec, "%s -DTM %s",command,remoteAddress);    // construct the execution line.
  333.     
  334.     // launch the remote
  335.     if ((fRexecSocket = rexec (&fExmachine, (short) 512, fExuser, fExpass,
  336.                 exec, NULL)) < 0)
  337.         return -1;
  338.         
  339.     count = TickCount ();    // counter
  340.         
  341.     // wait for a response
  342.     count += 3600;                                    // counter
  343.     while (!DTMavailRead(fInport))                // anything there?
  344.         {
  345.         currentTick = TickCount();                    // current tick.
  346.         if (TickCount () > count) 
  347.             return -1;
  348.         else
  349.             {
  350.             if (IsCancelEvent()) 
  351.                 return -1;
  352.             //mNextCursor (count+currentTick);
  353.             }
  354.         }
  355.     
  356.     // read the address. 
  357.     if (DTMbeginRead (fInport, exec, 256) == DTMERROR)
  358.         {
  359.         DTMendRead (fInport);
  360.         return -1;
  361.         }
  362.         
  363.     DTMendRead (fInport);
  364.     sscanf (exec, "DTM %s", exec);
  365.     fOutport = DTMmakeOutPort (exec);
  366.     
  367.     if (fOutport == -1)
  368.         // couldn't get the remote port.
  369.         return -1;
  370. }
  371.  
  372. // --------------------------------------------------------------------------------------------------
  373. //    DataReady
  374. //     is there data to receive.
  375. // --------------------------------------------------------------------------------------------------
  376. int TDataTransport::DataReady()
  377. {
  378.     
  379.     if (fInport != -1)
  380.         if (DTMavailRead(fInport))
  381.             {
  382.             // attempt to read the header of the DTM message 
  383.             if (DTMbeginRead(fInport, fHeader, DTM_MAX_HEADER) == DTMERROR)
  384.                 return -1;
  385.                 
  386.             if (DTMcompareClass (fHeader, "MSG"))
  387.                 return 1;
  388.             else 
  389.                 if (DTMcompareClass (fHeader, SDSclass))
  390.                     return 2;
  391.             }
  392.         else
  393.             return 0;
  394. }
  395.  
  396. // --------------------------------------------------------------------------------------------------
  397. //    RecieveMessage
  398. //    This routine will read an incoming message and return a pointer to the message.
  399. // --------------------------------------------------------------------------------------------------
  400. int TDataTransport::ReceiveMessage (char * message)
  401.     {
  402.     strcpy (message, fHeader+3);
  403.     while (DTMrecvDataset(fInport, fHeader, DTM_MAX_HEADER, DTM_CHAR) > 0);
  404.     DTMendRead(fInport);
  405.     return 1;
  406.     }
  407.  
  408. // --------------------------------------------------------------------------------------------------
  409. //    SendDTMMessage
  410. //            This routine will send a DTM message on a remote machine.
  411. // --------------------------------------------------------------------------------------------------
  412. int TDataTransport::SendMessage(char *command)
  413.     {
  414.     char  h[DTM_MAX_HEADER];
  415.     
  416.     // initialize the DTM ports listed on the command line.
  417.     strcpy(h, "MSG ");
  418.     strcat(h, command);
  419.     
  420.     if (DTMbeginWrite(fOutport, h, strlen(h)+1) == DTMERROR)
  421.         return 0;
  422.         
  423.     DTMendWrite(fOutport);
  424.     return 1;
  425.     }
  426.  
  427. // --------------------------------------------------------------------------------------------------
  428. //    RecieveRasterData
  429. //     this routine will read a raster image group. The TRstrData is
  430. //         expected to be allocated beforehand. The storage for the data
  431. //         however is allocated by this routine.
  432. // --------------------------------------------------------------------------------------------------
  433. int TDataTransport::ReceiveRasterData (TRasterData * RstrData)
  434.     {
  435. //    int32    dims[3] = {1, 1, 1};
  436. //    int        type, rank = 3;
  437.     
  438. //    SDSgetType (fHeader, &type);
  439. //    SDSgetDimensions(fHeader, &rank, fDims);
  440. //    SDSgetTitle (fHeader, fTitle, sizeof(fTitle));
  441.     
  442.     /* read dataset */
  443.     
  444. //    if (type == DTM_CHAR)
  445. //        {
  446. //        fRstr = NewHandle (fDims[0] * fDims[1]);
  447. //        if (fRstr)
  448. //            return 0;
  449.             
  450.         /* read the data. */
  451.     
  452. //        HLock (fRstr);
  453. //        DTMrecvDataset(fInport, *fRstr, dims[0] * dims[1], type);
  454. //        HUnlock (fRstr);
  455. //        return 1;
  456. //        }
  457.     return 0;
  458.     }
  459.  
  460. // --------------------------------------------------------------------------------------------------
  461. //        EndDTM
  462. //        Trash the DTM connections.
  463. // --------------------------------------------------------------------------------------------------
  464. int TDataTransport::EndDTM ()                                                            /* kill the DTM ports. */
  465.     {
  466.     if (fInport != -1)
  467.         DTMdestroyPort(fInport);
  468.         
  469.     if (fOutport != -1)
  470.         DTMdestroyPort(fOutport);
  471.  
  472.     fInport = -1;
  473.     fOutport = -1;
  474.     
  475.     /* kill the rexec port. */
  476.     
  477. //    if (fRexecSocket > -1)
  478. //        s_close (fRexecSocket);
  479.     return 0;
  480.     }
  481.  
  482. // **********************************************************************
  483. // These are the methods for TRasterData
  484. // **********************************************************************
  485. // --------------------------------------------------------------------------------------------------
  486. //    WriteRIS
  487. //     This routine will write the scientific data out to an
  488. //         hdf file. The name of the file is contained in the RstrData
  489. //         field fFileName. The caller must fill this value in.
  490. // --------------------------------------------------------------------------------------------------
  491.  
  492. int TRasterData::WriteRIS ()
  493.     {
  494. //    int lastref;    // contains the last reference number to identify the RI8
  495.     
  496.     // if there is floating point data write it out to the file too.
  497. //    HLock ((Handle) fRstr);
  498. //    DFR8addimage (fFileName, *fRstr, fDims[0], fDims[1], DFTAG_RLE);
  499. //    HUnlock ((Handle) fRstr);
  500.     
  501.     // do the annotation, the label for the file.
  502. //    lastref = DFR8lastref();
  503. //    DFANputlabel (fFileName, DFTAG_RI8, lastref, fTitle);
  504.     
  505.     // set up the file type.
  506. //    SetCreatorType (fFileName, '????', '_HDF');
  507.     return 1;
  508.     }
  509.  
  510.  
  511.     
  512.  
  513.